<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    

    

    



    <meta charset="utf-8">
    
    
    <meta name="sogou_site_verification" content="true">
    
    
    
    <title>全文搜索引擎Elasticsearch学习 | Lvshen&#39;s Blog | This is My World</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="Elasticsearch,搜索">
    <meta name="baidu-site-verification" content="VIVNdSiMZm">
    <meta name="description" content="作者：阮一峰 原文地址：http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html  全文搜索属于最常见的需求，开源的 Elasticsearch （以下简称 Elastic）是目前全文搜索引擎的首选。 它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。">
<meta name="keywords" content="Elasticsearch,搜索">
<meta property="og:type" content="article">
<meta property="og:title" content="全文搜索引擎Elasticsearch学习">
<meta property="og:url" content="https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/index.html">
<meta property="og:site_name" content="Lvshen&#39;s Blog">
<meta property="og:description" content="作者：阮一峰 原文地址：http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html  全文搜索属于最常见的需求，开源的 Elasticsearch （以下简称 Elastic）是目前全文搜索引擎的首选。 它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://www.ruanyifeng.com/blogimg/asset/2017/bg2017081701.jpg">
<meta property="og:updated_time" content="2017-10-17T01:58:45.498Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="全文搜索引擎Elasticsearch学习">
<meta name="twitter:description" content="作者：阮一峰 原文地址：http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html  全文搜索属于最常见的需求，开源的 Elasticsearch （以下简称 Elastic）是目前全文搜索引擎的首选。 它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。">
<meta name="twitter:image" content="http://www.ruanyifeng.com/blogimg/asset/2017/bg2017081701.jpg">
    
    <link rel="shortcut icon" href="/img/mylogo.jpg">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">我的技术小房间</h5>
          <a href="mailto:https://lvshen9.github.io" title="https://lvshen9.github.io" class="mail">https://lvshen9.github.io</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/about"  >
                <i class="icon icon-lg icon-address-book"></i>
                About
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/collection"  >
                <i class="icon icon-lg icon-apple"></i>
                Collection
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://lvshen9.github.io/" target="_blank" >
                <i class="icon icon-lg icon-wordpress"></i>
                Blog
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/lvshen9" target="_blank" >
                <i class="icon icon-lg icon-github-alt"></i>
                GitHub
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">全文搜索引擎Elasticsearch学习</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">全文搜索引擎Elasticsearch学习</h1>
        <h5 class="subtitle">
            
                <time datetime="2017-10-17T01:26:31.000Z" itemprop="datePublished" class="page-time">
  2017-10-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#一、安装"><span class="post-toc-number">1.</span> <span class="post-toc-text">一、安装</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#二、基本概念"><span class="post-toc-number">2.</span> <span class="post-toc-text">二、基本概念</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-1-Node-与-Cluster"><span class="post-toc-number">2.1.</span> <span class="post-toc-text">2.1 Node 与 Cluster</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-2-Index"><span class="post-toc-number">2.2.</span> <span class="post-toc-text">2.2 Index</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-3-Document"><span class="post-toc-number">2.3.</span> <span class="post-toc-text">2.3 Document</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#2-4-Type"><span class="post-toc-number">2.4.</span> <span class="post-toc-text">2.4 Type</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#三、新建和删除-Index"><span class="post-toc-number">3.</span> <span class="post-toc-text">三、新建和删除 Index</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#四、中文分词设置"><span class="post-toc-number">4.</span> <span class="post-toc-text">四、中文分词设置</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#五、数据操作"><span class="post-toc-number">5.</span> <span class="post-toc-text">五、数据操作</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#5-1-新增记录"><span class="post-toc-number">5.1.</span> <span class="post-toc-text">5.1 新增记录</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#5-2-查看记录"><span class="post-toc-number">5.2.</span> <span class="post-toc-text">5.2 查看记录</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#5-3-删除记录"><span class="post-toc-number">5.3.</span> <span class="post-toc-text">5.3 删除记录</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#5-4-更新记录"><span class="post-toc-number">5.4.</span> <span class="post-toc-text">5.4 更新记录</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#六、数据查询"><span class="post-toc-number">6.</span> <span class="post-toc-text">六、数据查询</span></a><ol class="post-toc-child"><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-1-返回所有记录"><span class="post-toc-number">6.1.</span> <span class="post-toc-text">6.1 返回所有记录</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-2-全文搜索"><span class="post-toc-number">6.2.</span> <span class="post-toc-text">6.2 全文搜索</span></a></li><li class="post-toc-item post-toc-level-5"><a class="post-toc-link" href="#6-3-逻辑运算"><span class="post-toc-number">6.3.</span> <span class="post-toc-text">6.3 逻辑运算</span></a></li></ol></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#七、参考链接"><span class="post-toc-number">7.</span> <span class="post-toc-text">七、参考链接</span></a></li></ol>
        </nav>
    </aside>


<article id="post-全文搜索引擎Elasticsearch学习"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">全文搜索引擎Elasticsearch学习</h1>
        <div class="post-meta">
            <time class="post-time" title="2017-10-17 09:26:31" datetime="2017-10-17T01:26:31.000Z"  itemprop="datePublished">2017-10-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/categories/技术/">技术</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <blockquote>
<p>作者：阮一峰 原文地址：<a href="http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html" target="_blank" rel="noopener">http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html</a></p>
</blockquote>
<p>全文搜索属于最常见的需求，开源的 Elasticsearch （以下简称 Elastic）是目前全文搜索引擎的首选。</p>
<p>它可以快速地储存、搜索和分析海量数据。维基百科、Stack Overflow、Github 都采用它。<figure class="image-bubble">
                <div class="img-lightbox">
                    <div class="overlay"></div>
                    <img src="http://www.ruanyifeng.com/blogimg/asset/2017/bg2017081701.jpg" alt title>
                </div>
                <div class="image-caption"></div>
            </figure></p>
<p>Elastic 的底层是开源库 <a href="https://lucene.apache.org/" target="_blank" rel="noopener">Lucene</a>。但是，你没法直接用 Lucene，必须自己写代码去调用它的接口。Elastic 是 Lucene 的封装，提供了 REST API 的操作接口，开箱即用。</p>
<p>本文从零开始，讲解如何使用 Elastic 搭建自己的全文搜索引擎。每一步都有详细的说明，大家跟着做就能学会。</p>
<a id="more"></a>
<h4 id="一、安装"><a href="#一、安装" class="headerlink" title="一、安装"></a>一、安装</h4><p>Elastic 需要 Java 8 环境。如果你的机器还没安装 Java，可以参考<a href="https://www.digitalocean.com/community/tutorials/how-to-install-java-with-apt-get-on-debian-8" target="_blank" rel="noopener">这篇文章</a>，注意要保证环境变量<code>JAVA_HOME</code>正确设置。</p>
<p>安装完 Java，就可以跟着<a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html" target="_blank" rel="noopener">官方文档</a>安装 Elastic。直接下载压缩包比较简单。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.1.zip</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> unzip elasticsearch-5.5.1.zip</span></span><br><span class="line"><span class="meta">$</span><span class="bash"> <span class="built_in">cd</span> elasticsearch-5.5.1/</span></span><br></pre></td></tr></table></figure>
<p>接着，进入解压后的目录，运行下面的命令，启动 Elastic。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> ./bin/elasticsearch</span></span><br></pre></td></tr></table></figure>
<p>如果这时<a href="https://github.com/spujadas/elk-docker/issues/92" target="_blank" rel="noopener">报错</a><code>&quot;max virtual memory areas vm.max*map*count [65530] is too low&quot;</code>，要运行下面的命令。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> sudo sysctl -w vm.max_map_count=262144</span></span><br></pre></td></tr></table></figure>
<p>如果一切正常，Elastic 就会在默认的9200端口运行。这时，打开另一个命令行窗口，请求该端口，会得到说明信息。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl localhost:9200</span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  "name" : "atntrTf",</span><br><span class="line">  "cluster_name" : "elasticsearch",</span><br><span class="line">  "cluster_uuid" : "tf9250XhQ6ee4h7YI11anA",</span><br><span class="line">  "version" : &#123;</span><br><span class="line">    "number" : "5.5.1",</span><br><span class="line">    "build_hash" : "19c13d0",</span><br><span class="line">    "build_date" : "2017-07-18T20:44:24.823Z",</span><br><span class="line">    "build_snapshot" : false,</span><br><span class="line">    "lucene_version" : "6.6.0"</span><br><span class="line">  &#125;,</span><br><span class="line">  "tagline" : "You Know, for Search"</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面代码中，请求9200端口，Elastic 返回一个 JSON 对象，包含当前节点、集群、版本等信息。</p>
<p>按下 Ctrl + C，Elastic 就会停止运行。</p>
<p>默认情况下，Elastic 只允许本机访问，如果需要远程访问，可以修改 Elastic 安装目录的<code>config/elasticsearch.yml</code>文件，去掉<code>network.host</code>的注释，将它的值改成<code>0.0.0.0</code>，然后重新启动 Elastic。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">network.host: 0.0.0.0</span><br></pre></td></tr></table></figure>
<p>上面代码中，设成<code>0.0.0.0</code>让任何人都可以访问。线上服务不要这样设置，要设成具体的 IP。</p>
<h4 id="二、基本概念"><a href="#二、基本概念" class="headerlink" title="二、基本概念"></a>二、基本概念</h4><h5 id="2-1-Node-与-Cluster"><a href="#2-1-Node-与-Cluster" class="headerlink" title="2.1 Node 与 Cluster"></a>2.1 Node 与 Cluster</h5><p>Elastic 本质上是一个分布式数据库，允许多台服务器协同工作，每台服务器可以运行多个 Elastic 实例。</p>
<p>单个 Elastic 实例称为一个节点（node）。一组节点构成一个集群（cluster）。</p>
<h5 id="2-2-Index"><a href="#2-2-Index" class="headerlink" title="2.2 Index"></a>2.2 Index</h5><p>Elastic 会索引所有字段，经过处理后写入一个反向索引（Inverted Index）。查找数据的时候，直接查找该索引。</p>
<p>所以，Elastic 数据管理的顶层单位就叫做 Index（索引）。它是单个数据库的同义词。每个 Index （即数据库）的名字必须是小写。</p>
<p>下面的命令可以查看当前节点的所有 Index。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X GET <span class="string">'http://localhost:9200/_cat/indices?v'</span></span></span><br></pre></td></tr></table></figure>
<h5 id="2-3-Document"><a href="#2-3-Document" class="headerlink" title="2.3 Document"></a>2.3 Document</h5><p>Index 里面单条的记录称为 Document（文档）。许多条 Document 构成了一个 Index。</p>
<p>Document 使用 JSON 格式表示，下面是一个例子。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "user": "张三",</span><br><span class="line">  "title": "工程师",</span><br><span class="line">  "desc": "数据库管理"</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>同一个 Index 里面的 Document，不要求有相同的结构（scheme），但是最好保持相同，这样有利于提高搜索效率。</p>
<h5 id="2-4-Type"><a href="#2-4-Type" class="headerlink" title="2.4 Type"></a>2.4 Type</h5><p>Document 可以分组，比如<code>weather</code>这个 Index 里面，可以按城市分组（北京和上海），也可以按气候分组（晴天和雨天）。这种分组就叫做 Type，它是虚拟的逻辑分组，用来过滤 Document。</p>
<p>不同的 Type 应该有相似的结构（schema），举例来说，<code>id</code>字段不能在这个组是字符串，在另一个组是数值。这是与关系型数据库的表的<a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping.html" target="_blank" rel="noopener">一个区别</a>。性质完全不同的数据（比如<code>products</code>和<code>logs</code>）应该存成两个 Index，而不是一个 Index 里面的两个 Type（虽然可以做到）。</p>
<p>下面的命令可以列出每个 Index 所包含的 Type。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/_mapping?pretty=true'</span></span></span><br></pre></td></tr></table></figure>
<p>根据<a href="https://www.elastic.co/blog/index-type-parent-child-join-now-future-in-elasticsearch" target="_blank" rel="noopener">规划</a>，Elastic 6.x 版只允许每个 Index 包含一个 Type，7.x 版将会彻底移除 Type。</p>
<h4 id="三、新建和删除-Index"><a href="#三、新建和删除-Index" class="headerlink" title="三、新建和删除 Index"></a>三、新建和删除 Index</h4><p>新建 Index，可以直接向 Elastic 服务器发出 PUT 请求。下面的例子是新建一个名叫<code>weather</code>的 Index。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X PUT <span class="string">'localhost:9200/weather'</span></span></span><br></pre></td></tr></table></figure>
<p>服务器返回一个 JSON 对象，里面的<code>acknowledged</code>字段表示操作成功。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "acknowledged":true,</span><br><span class="line">  "shards_acknowledged":true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后，我们发出 DELETE 请求，删除这个 Index。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X DELETE <span class="string">'localhost:9200/weather'</span></span></span><br></pre></td></tr></table></figure>
<h4 id="四、中文分词设置"><a href="#四、中文分词设置" class="headerlink" title="四、中文分词设置"></a>四、中文分词设置</h4><p>首先，安装中文分词插件。这里使用的是 <a href="https://github.com/medcl/elasticsearch-analysis-ik/" target="_blank" rel="noopener">ik</a>，也可以考虑其他插件（比如 <a href="https://www.elastic.co/guide/en/elasticsearch/plugins/current/analysis-smartcn.html" target="_blank" rel="noopener">smartcn</a>）。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.1/elasticsearch-analysis-ik-5.5.1.zip</span></span><br></pre></td></tr></table></figure>
<p>上面代码安装的是5.5.1版的插件，与 Elastic 5.5.1 配合使用。</p>
<p>接着，重新启动 Elastic，就会自动加载这个新安装的插件。</p>
<p>然后，新建一个 Index，指定需要分词的字段。这一步根据数据结构而异，下面的命令只针对本文。基本上，凡是需要搜索的中文字段，都要单独设置一下。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X PUT <span class="string">'localhost:9200/accounts'</span> -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "mappings": &#123;</span><br><span class="line">    "person": &#123;</span><br><span class="line">      "properties": &#123;</span><br><span class="line">        "user": &#123;</span><br><span class="line">          "type": "text",</span><br><span class="line">          "analyzer": "ik_max_word",</span><br><span class="line">          "search_analyzer": "ik_max_word"</span><br><span class="line">        &#125;,</span><br><span class="line">        "title": &#123;</span><br><span class="line">          "type": "text",</span><br><span class="line">          "analyzer": "ik_max_word",</span><br><span class="line">          "search_analyzer": "ik_max_word"</span><br><span class="line">        &#125;,</span><br><span class="line">        "desc": &#123;</span><br><span class="line">          "type": "text",</span><br><span class="line">          "analyzer": "ik_max_word",</span><br><span class="line">          "search_analyzer": "ik_max_word"</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码中，首先新建一个名称为<code>accounts</code>的 Index，里面有一个名称为<code>person</code>的 Type。<code>person</code>有三个字段。</p>
<blockquote>
<ul>
<li>user</li>
<li>title</li>
<li>desc</li>
</ul>
</blockquote>
<p>这三个字段都是中文，而且类型都是文本（text），所以需要指定中文分词器，不能使用默认的英文分词器。</p>
<p>Elastic 的分词器称为 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html" target="_blank" rel="noopener">analyzer</a>。我们对每个字段指定分词器。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">"user": &#123;</span><br><span class="line">  "type": "text",</span><br><span class="line">  "analyzer": "ik_max_word",</span><br><span class="line">  "search_analyzer": "ik_max_word"</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面代码中，<code>analyzer</code>是字段文本的分词器，<code>search_analyzer</code>是搜索词的分词器。<code>ik_max_word</code>分词器是插件<code>ik</code>提供的，可以对文本进行最大数量的分词。</p>
<h4 id="五、数据操作"><a href="#五、数据操作" class="headerlink" title="五、数据操作"></a>五、数据操作</h4><h5 id="5-1-新增记录"><a href="#5-1-新增记录" class="headerlink" title="5.1 新增记录"></a>5.1 新增记录</h5><p>向指定的 /Index/Type 发送 PUT 请求，就可以在 Index 里面新增一条记录。比如，向<code>/accounts/person</code>发送请求，就可以新增一条人员记录。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X PUT <span class="string">'localhost:9200/accounts/person/1'</span> -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "user": "张三",</span><br><span class="line">  "title": "工程师",</span><br><span class="line">  "desc": "数据库管理"</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>服务器返回的 JSON 对象，会给出 Index、Type、Id、Version 等信息。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "_index":"accounts",</span><br><span class="line">  "_type":"person",</span><br><span class="line">  "_id":"1",</span><br><span class="line">  "_version":1,</span><br><span class="line">  "result":"created",</span><br><span class="line">  "_shards":&#123;"total":2,"successful":1,"failed":0&#125;,</span><br><span class="line">  "created":true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果你仔细看，会发现请求路径是<code>/accounts/person/1</code>，最后的<code>1</code>是该条记录的 Id。它不一定是数字，任意字符串（比如<code>abc</code>）都可以。</p>
<p>新增记录的时候，也可以不指定 Id，这时要改成 POST 请求。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X POST <span class="string">'localhost:9200/accounts/person'</span> -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "user": "李四",</span><br><span class="line">  "title": "工程师",</span><br><span class="line">  "desc": "系统管理"</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码中，向<code>/accounts/person</code>发出一个 POST 请求，添加一个记录。这时，服务器返回的 JSON 对象里面，<code>_id</code>字段就是一个随机字符串。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "_index":"accounts",</span><br><span class="line">  "_type":"person",</span><br><span class="line">  "_id":"AV3qGfrC6jMbsbXb6k1p",</span><br><span class="line">  "_version":1,</span><br><span class="line">  "result":"created",</span><br><span class="line">  "_shards":&#123;"total":2,"successful":1,"failed":0&#125;,</span><br><span class="line">  "created":true</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>注意，如果没有先创建 Index（这个例子是<code>accounts</code>），直接执行上面的命令，Elastic 也不会报错，而是直接生成指定的 Index。所以，打字的时候要小心，不要写错 Index 的名称。</p>
<h5 id="5-2-查看记录"><a href="#5-2-查看记录" class="headerlink" title="5.2 查看记录"></a>5.2 查看记录</h5><p>向<code>/Index/Type/Id</code>发出 GET 请求，就可以查看这条记录。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/1?pretty=true'</span></span></span><br></pre></td></tr></table></figure>
<p>上面代码请求查看<code>/accounts/person/1</code>这条记录，URL 的参数<code>pretty=true</code>表示以易读的格式返回。</p>
<p>返回的数据中，<code>found</code>字段表示查询成功，<code>_source</code>字段返回原始记录。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "_index" : "accounts",</span><br><span class="line">  "_type" : "person",</span><br><span class="line">  "_id" : "1",</span><br><span class="line">  "_version" : 1,</span><br><span class="line">  "found" : true,</span><br><span class="line">  "_source" : &#123;</span><br><span class="line">    "user" : "张三",</span><br><span class="line">    "title" : "工程师",</span><br><span class="line">    "desc" : "数据库管理"</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>如果 Id 不正确，就查不到数据，<code>found</code>字段就是<code>false</code>。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/weather/beijing/abc?pretty=true'</span></span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  "_index" : "accounts",</span><br><span class="line">  "_type" : "person",</span><br><span class="line">  "_id" : "abc",</span><br><span class="line">  "found" : false</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="5-3-删除记录"><a href="#5-3-删除记录" class="headerlink" title="5.3 删除记录"></a>5.3 删除记录</h5><p>删除记录就是发出 DELETE 请求。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X DELETE <span class="string">'localhost:9200/accounts/person/1'</span></span></span><br></pre></td></tr></table></figure>
<p>这里先不要删除这条记录，后面还要用到。</p>
<h5 id="5-4-更新记录"><a href="#5-4-更新记录" class="headerlink" title="5.4 更新记录"></a>5.4 更新记录</h5><p>更新记录就是使用 PUT 请求，重新发送一次数据。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl -X PUT <span class="string">'localhost:9200/accounts/person/1'</span> -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">    "user" : "张三",</span><br><span class="line">    "title" : "工程师",</span><br><span class="line">    "desc" : "数据库管理，软件开发"</span><br><span class="line">&#125;' </span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  "_index":"accounts",</span><br><span class="line">  "_type":"person",</span><br><span class="line">  "_id":"1",</span><br><span class="line">  "_version":2,</span><br><span class="line">  "result":"updated",</span><br><span class="line">  "_shards":&#123;"total":2,"successful":1,"failed":0&#125;,</span><br><span class="line">  "created":false</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面代码中，我们将原始数据从”数据库管理”改成”数据库管理，软件开发”。 返回结果里面，有几个字段发生了变化。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">"_version" : 2,</span><br><span class="line">"result" : "updated",</span><br><span class="line">"created" : false</span><br></pre></td></tr></table></figure>
<p>可以看到，记录的 Id 没变，但是版本（version）从<code>1</code>变成<code>2</code>，操作类型（result）从<code>created</code>变成<code>updated</code>，<code>created</code>字段变成<code>false</code>，因为这次不是新建记录。</p>
<h4 id="六、数据查询"><a href="#六、数据查询" class="headerlink" title="六、数据查询"></a>六、数据查询</h4><h5 id="6-1-返回所有记录"><a href="#6-1-返回所有记录" class="headerlink" title="6.1 返回所有记录"></a>6.1 返回所有记录</h5><p>使用 GET 方法，直接请求<code>/Index/Type/_search</code>，就会返回所有记录。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span></span></span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">  "took":2,</span><br><span class="line">  "timed_out":false,</span><br><span class="line">  "_shards":&#123;"total":5,"successful":5,"failed":0&#125;,</span><br><span class="line">  "hits":&#123;</span><br><span class="line">    "total":2,</span><br><span class="line">    "max_score":1.0,</span><br><span class="line">    "hits":[</span><br><span class="line">      &#123;</span><br><span class="line">        "_index":"accounts",</span><br><span class="line">        "_type":"person",</span><br><span class="line">        "_id":"AV3qGfrC6jMbsbXb6k1p",</span><br><span class="line">        "_score":1.0,</span><br><span class="line">        "_source": &#123;</span><br><span class="line">          "user": "李四",</span><br><span class="line">          "title": "工程师",</span><br><span class="line">          "desc": "系统管理"</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      &#123;</span><br><span class="line">        "_index":"accounts",</span><br><span class="line">        "_type":"person",</span><br><span class="line">        "_id":"1",</span><br><span class="line">        "_score":1.0,</span><br><span class="line">        "_source": &#123;</span><br><span class="line">          "user" : "张三",</span><br><span class="line">          "title" : "工程师",</span><br><span class="line">          "desc" : "数据库管理，软件开发"</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>上面代码中，返回结果的 <code>took</code>字段表示该操作的耗时（单位为毫秒），<code>timed_out</code>字段表示是否超时，<code>hits</code>字段表示命中的记录，里面子字段的含义如下。</p>
<blockquote>
<ul>
<li><code>total</code>：返回记录数，本例是2条。</li>
<li><code>max_score</code>：最高的匹配程度，本例是<code>1.0</code>。</li>
<li><code>hits</code>：返回的记录组成的数组。</li>
</ul>
</blockquote>
<p>返回的记录中，每条记录都有一个<code>_score</code>字段，表示匹配的程序，默认是按照这个字段降序排列。</p>
<h5 id="6-2-全文搜索"><a href="#6-2-全文搜索" class="headerlink" title="6.2 全文搜索"></a>6.2 全文搜索</h5><p>Elastic 的查询非常特别，使用自己的<a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl.html" target="_blank" rel="noopener">查询语法</a>，要求 GET 请求带有数据体。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span>  -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "query" : &#123; "match" : &#123; "desc" : "软件" &#125;&#125;</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码使用 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-match-query.html" target="_blank" rel="noopener">Match 查询</a>，指定的匹配条件是<code>desc</code>字段里面包含”软件”这个词。返回结果如下。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  "took":3,</span><br><span class="line">  "timed_out":false,</span><br><span class="line">  "_shards":&#123;"total":5,"successful":5,"failed":0&#125;,</span><br><span class="line">  "hits":&#123;</span><br><span class="line">    "total":1,</span><br><span class="line">    "max_score":0.28582606,</span><br><span class="line">    "hits":[</span><br><span class="line">      &#123;</span><br><span class="line">        "_index":"accounts",</span><br><span class="line">        "_type":"person",</span><br><span class="line">        "_id":"1",</span><br><span class="line">        "_score":0.28582606,</span><br><span class="line">        "_source": &#123;</span><br><span class="line">          "user" : "张三",</span><br><span class="line">          "title" : "工程师",</span><br><span class="line">          "desc" : "数据库管理，软件开发"</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    ]</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>Elastic 默认一次返回10条结果，可以通过<code>size</code>字段改变这个设置。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span>  -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "query" : &#123; "match" : &#123; "desc" : "管理" &#125;&#125;,</span><br><span class="line">  "size": 1</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码指定，每次只返回一条结果。</p>
<p>还可以通过<code>from</code>字段，指定位移。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span>  -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "query" : &#123; "match" : &#123; "desc" : "管理" &#125;&#125;,</span><br><span class="line">  "from": 1,</span><br><span class="line">  "size": 1</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码指定，从位置1开始（默认是从位置0开始），只返回一条结果。</p>
<h5 id="6-3-逻辑运算"><a href="#6-3-逻辑运算" class="headerlink" title="6.3 逻辑运算"></a>6.3 逻辑运算</h5><p>如果有多个搜索关键字， Elastic 认为它们是<code>or</code>关系。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span>  -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "query" : &#123; "match" : &#123; "desc" : "软件 系统" &#125;&#125;</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<p>上面代码搜索的是<code>软件 or 系统</code>。</p>
<p>如果要执行多个关键词的<code>and</code>搜索，必须使用<a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-bool-query.html" target="_blank" rel="noopener">布尔查询</a>。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl <span class="string">'localhost:9200/accounts/person/_search'</span>  -d <span class="string">'</span></span></span><br><span class="line">&#123;</span><br><span class="line">  "query": &#123;</span><br><span class="line">    "bool": &#123;</span><br><span class="line">      "must": [</span><br><span class="line">        &#123; "match": &#123; "desc": "软件" &#125; &#125;,</span><br><span class="line">        &#123; "match": &#123; "desc": "系统" &#125; &#125;</span><br><span class="line">      ]</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;'</span><br></pre></td></tr></table></figure>
<h4 id="七、参考链接"><a href="#七、参考链接" class="headerlink" title="七、参考链接"></a>七、参考链接</h4><ul>
<li><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/getting-started.html" target="_blank" rel="noopener">ElasticSearch 官方手册</a></li>
<li><a href="https://www.elastic.co/blog/a-practical-introduction-to-elasticsearch" target="_blank" rel="noopener">A Practical Introduction to Elasticsearch</a></li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-10-17T01:58:45.498Z" itemprop="dateUpdated">2017-10-17 09:58:45</time>
</span><br>


        
        原文链接：<a href="/2017/10/17/全文搜索引擎Elasticsearch学习/" target="_blank" rel="external">https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/</a>
        
    </div>
    
    <footer>
        <a href="https://lvshen9.gitee.io">
            <img src="/img/avatar.jpg" alt="我的技术小房间">
            我的技术小房间
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Elasticsearch/">Elasticsearch</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/搜索/">搜索</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&title=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&title=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&source=
作者：阮一峰 原文地址：http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

全文搜索属..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2017/10/17/分布式与集群/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">分布式与集群</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2017/10/04/【算法】快速排序/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">【算法】快速排序</h4>
      </a>
    </div>
  
</nav>



    











    <!-- Valine Comments -->
    <div class="comments vcomment" id="comments"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
    <script src="//unpkg.com/valine@latest/dist/Valine.min.js"></script>
    <!-- Valine Comments script -->
    <script>
        var GUEST_INFO = ['nick','mail','link'];
        var guest_info = 'nick,mail,link'.split(',').filter(function(item){
          return GUEST_INFO.indexOf(item) > -1
        });
        new Valine({
            el: '#comments',
            notify: 'false' == 'true',
            verify: 'false' == 'true',
            appId: "dy9kXHwg5jQUlLryQmpjWRlM-gzGzoHsz",
            appKey: "P9Nh39Ol0JbMMiYqNGHEP3ml",
            avatar: "mm",
            placeholder: "Just go go",
            guest_info: guest_info.length == 0 ? GUEST_INFO : guest_info,
            pageSize: "10"
        })
    </script>
    <!-- Valine Comments end -->







</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="https://lvshen9.github.io/blog2/pay/weixin.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="https://lvshen9.github.io/blog2/pay/weixin.jpg" data-alipay="https://lvshen9.github.io/blog2/pay/zhifu.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>我的技术小房间 &copy; 2015 - 2020</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&title=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&pic=https://lvshen9.gitee.io/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&title=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&source=
作者：阮一峰 原文地址：http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html

全文搜索属..." data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《全文搜索引擎Elasticsearch学习》 — Lvshen's Blog&url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/&via=https://lvshen9.gitee.io" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://lvshen9.gitee.io/2017/10/17/全文搜索引擎Elasticsearch学习/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
